/**
* @description: 周赛308 t2
* @author hewei
* @date 2022/8/28 10:38
* @version 1.0
*/

public class RemoveStars {

    public static void main(String[] args) {
        RemoveStars removeStars = new RemoveStars();
        System.out.println(removeStars.removeStars("abb*cdfg*****x*"));
    }

    public String removeStars1(String s) {
        char[] chars = s.toCharArray();
        int count = 0;
        for (int i = chars.length - 1; i >= 0; ) {
            char c = chars[i];
            if (c == '*') {
                ++count;
                --i;
            } else {
                if (count == 0) --i;
                while (count > 0) {
                    if (chars[i] == '*') {
                        ++count;
                    } else {
                        chars[i] = '*';
                        --count;
                    }
                    --i;
                }
                count = 0;
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] != '*') stringBuilder.append(chars[i]);
        }
        return stringBuilder.toString();
    }

    public String removeStars(String s) {
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '*') ans.deleteCharAt(ans.length() - 1);
            else ans.append(c);
        }
        return ans.toString();
    }
}
